import { useUserStore } from "@/stores/user";
import request from "@/utils/request";
import axios from "axios";

// 模拟登陆，实际需要调用request进行登录请求
export function login(username, password) {
    let user_data=null
    console.log(username)
    console.log(password)
    
    
    return new Promise((resolve, reject) => {
        axios({
            method:'get',
            baseURL:'https://fitzone.work:8080/login',
            params:{
                email:username,
                pwd:password
            }
        }).then(res=>{
            user_data=res.data
            console.log(user_data)
        if (user_data.length!=0) {
            if(user_data.identity=="2"){
                resolve({
                code: 200,
                msg: '请求成功',
                token: user_data
            })
            }else{
                reject('You are not the user! Please login in web!');
            }
        }
        else {
            reject('E-mail or password is wrong');
        }
        })
        
    });
}

// 模拟请求用户信息，正常是需要经过后端校验token后判断登录用户再进行返回数据
export function getInfo(token) {
    const userStore = useUserStore();
    return new Promise((resolve, reject) => {
        if (token != null) {
            resolve({
                code: 200,
                msg: '请求成功',
                data: {
                    userId: token.id,
                    userName: token.email,
                    nickName: token.username,
                    userId: token.id,
                    avatar: 'https://fitzone.work/img/'+token.img,
                    permissions: [token.vip,token.balance],
                    roles: ['admin']
                }
            })
        } else if (token == 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJRUToyMzc5NzIxMTMiLCJuYW1lIjoidXNlcjAxIiwiaWF0IjoxNTE2MjM5MDIyfQ.7H5YJp0vmJym6cYp_pALEQEaj6pPZWVvea1vm8uIMJQ') {
            resolve({
                code: 200,
                msg: '请求成功',
                data: {
                    userId: 2,
                    userName: 'user1',
                    nickName: '用户1',
                    avatar: 'https://files.catbox.moe/zfaaqx.jpg',
                    permissions: ['test:list'],
                    roles: ['user1']
                }
            })
        } else {
            resolve({
                code: 403,
                msg: 'token已失效',
                data: {}
            })
        }

    });
}

// 模拟请求用户信息，正常是需要传入token后进行登出
export function logout(token) {
    return new Promise((resolve, reject) => {
        resolve({
            code: 200,
            msg: '退出成功'
        })
    });
}
//注册
export function signup(token) {
    return new Promise((resolve, reject) => {
        resolve({
            code: 200,
            msg: '注册跳转'
        })
    });
}
